package com.example.NotePad;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.example.NotePad.R.string;

import android.os.Bundle;
import android.util.Base64;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.R.integer;
import android.app.ListActivity;
import android.content.Intent;









//This activity can be divided into several parts and itself no longer needs to be existed .
public class GetNotesIndexActivity extends ListActivity{
	public static String[] ss = new String[100];
	private List<ListDetails> detailsList = null;
	ListDetails aListDetails = new ListDetails();
	private static final int GETLIST = 1;
	private static final int ABOUT = 2;
	private static final String LISTPATH ="/sdcard/1.txt"; 
	GetNoteListFunc aGetNoteListFunc = new GetNoteListFunc();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) { 
		// TODO Auto-generated method stub
		menu.add(0, GETLIST, 1, R.string.GetNotesIndexActivity_getList);
		menu.add(0, ABOUT, 2, R.string.GetNotesIndexActivity_about);
		return super.onCreateOptionsMenu(menu);
	}
	
	

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		
		
		if(item.getItemId() == GETLIST)
		{
			//pressed Get List
			List<ListDetails> listDetailsesDetails = aGetNoteListFunc.getNoteList();
			List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
			for (Iterator iterator = listDetailsesDetails.iterator(); iterator.hasNext();) {
				ListDetails listDetails = (ListDetails) iterator.next();
				
				
				
				String decodeContentString = new String(Base64.decode(aListDetails.getNoteName(), Base64.DEFAULT));
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("notes_id", aListDetails.getNoteId());
				map.put("notes_name", decodeContentString);
				list.add(map);
			}
			
			SimpleAdapter simpleAdapter = new SimpleAdapter(this, list, R.layout.details, new String[]{"notes_id","notes_name"}, new int[]{R.id.notes_id,R.id.notes_name});
			setListAdapter(simpleAdapter);
		}
		else if(item.getItemId() == ABOUT)
		{
			//pressed about
			
		}
		
		return super.onOptionsItemSelected(item);
	}

	  
	/*public List<ListDetails> getNoteList(){ 
		  detailsList =  new ArrayList<ListDetails>(); 
		  try { 
			  
			   FileReader read = new FileReader(LISTPATH); 
			   BufferedReader br = new BufferedReader(read); 
			   String row; 
			   while((row = br.readLine())!=null){ 
			    System.out.println(row); 
			    ss = row.split(",");
			    for (int i = 0; i < ss.length; i++) 
			    	System.out.println(ss[i]);			    	


			    
//			    ListDetails aListDetails = new ListDetails();
			    aListDetails.setNoteId(ss[0]);			     
			    aListDetails.setNoteName(ss[1]);
			    aListDetails.setNoteContent(ss[2]);
			    detailsList.add(aListDetails);
			   
		   }
		  } catch (FileNotFoundException e) { 
		   e.printStackTrace(); 
		  } catch (IOException e){ 
		   e.printStackTrace(); 
		  }
		return detailsList;
		  
		}

	
	//pass the key of the selected item to the next activity which display the details of the notes.
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		
		//check if the detailsList if has something in it
		if(detailsList != null){
			ListDetails detailListDetails = detailsList.get(position);
			Intent intent = new Intent();
			intent.putExtra("detail", detailListDetails.getNoteId());
//			intent.setClass(this, DetailActivity.class);
			startActivity(intent);
		}
		super.onListItemClick(l, v, position, id);
	}

	
	*/
}
